#include "progresswidget.h"
#include "ui_progresswidget.h"

ProgressWidget::ProgressWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ProgressWidget)
{
    ui->setupUi(this);
    wiringPiSetup();
    pinMode(1,INPUT);  //初始化1针脚为输入
    //设置窗口标题
    setWindowTitle("水果机器人控制系统");

    //设置窗口背景-尺寸自适应
    m_bg = new QLabel(this);
    m_bg->setPixmap(QPixmap(":/bg.jpg"));
    m_bg->setScaledContents(true);
    m_bg->lower();

    QFont font1("SimSun", 30);
    QFont font2("SimHei",40);
    ui->titlelabel->setText(title1);
    ui->titlelabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    ui->titlelabel->setFont(font2);
    ui->titlelabel->repaint();
    ui->Textlabel->setText(text0);
    ui->Textlabel->setFont(font1);
    ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
    ui->Textlabel->repaint();
    ui->pgBarClean->setValue(0);
    ui->pgBarPeel->setValue(0);
    ui->pgBarCut->setValue(0);


    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeoutslot()));
    //while(!(digitalRead(1)==1));  //1引脚为低时，等待
    timer->start(100); //0.1s per loop

}

void ProgressWidget::resizeEvent(QResizeEvent *event)
{  //实现自适应窗口大小
    m_bg->resize(this->size());
    ui->horizontalLayoutWidget->resize(this->size());
}

void ProgressWidget::timeoutslot()
{
    if(startcheck == 0)
    {
        if(digitalRead(1) == 1)
        {
            startcheck = 1;
        }
        //qDebug() << digitalRead(1) << endl;
    }
    else
    {
        timeCount++;
    }
    if(timeCount == 0)
        return;
    if(timeCount <= 10 * time1)
    {
        if(check == 0)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text5);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            ui->titlelabel->setText(title2);
            ui->titlelabel->repaint();
            check++;
        }
    }
    else if(timeCount <= 10 * time2)
    {
        if(check == 1)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text1);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            check++;
        }
        per = 10 * (timeCount - 10 * time1)/(time2 - time1);
        ui->pgBarClean->setValue(per);
    }
    else if(timeCount <= 10 * time3)
    {
        if(check == 2)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text5);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            check++;
        }
    }
    else if(timeCount <= 10 * time4)
    {
        if(check == 3)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text2);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            check++;
        }
        per = 10 * (timeCount - 10 * time3)/(time4 - time3);
        ui->pgBarPeel->setValue(per);
    }
    else if(timeCount <= 10 * time5)
    {
        if(check == 4)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text5);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            check++;
        }
    }
    else if(timeCount <= 10 * time6)
    {
        if(check == 5)
        {
            QFont font1("SimSun", 30);
            ui->Textlabel->clear();
            ui->Textlabel->setText(text3);
            ui->Textlabel->setFont(font1);
            ui->Textlabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
            ui->Textlabel->repaint();
            check++;
        }
        per = 10 * (timeCount - 10 * time5)/(time6 - time5);
        ui->pgBarCut->setValue(per);
    }
    else
    {
        ui->Textlabel->setText(text4);
        ui->titlelabel->setText(title1);
        ui->Textlabel->repaint();
        ui->titlelabel->repaint();
        if(digitalRead(1) == 0)
        {
            check = 0;
            startcheck = 0;
            timeCount = 0;
            ui->titlelabel->setText(title1);
            ui->titlelabel->repaint();
            ui->Textlabel->setText(text0);
            ui->Textlabel->repaint();
            ui->pgBarClean->setValue(0);
            ui->pgBarPeel->setValue(0);
            ui->pgBarCut->setValue(0);
        }
    }
    //清洗 去皮 切割
}

ProgressWidget::~ProgressWidget()
{
    delete ui;
}
